package com.example.demo08;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebViewProvider;

import com.edroid.common.utils.ReflectHelper;

public class WebPage extends Fragment {
	WebView web;
	Rect r = new Rect();

	void proxy(final WebView webView) {
		try {
			final WebViewProvider mProvider = ReflectHelper.getF(webView, "mProvider");
			if(Proxy.isProxyClass(mProvider.getClass()))
				return;
			
			Log.i("11webview", "mProvider=" + mProvider);
			Object proxy = Proxy.newProxyInstance(WebViewProvider.class.getClassLoader(), 
					new Class[]{WebViewProvider.class}, 
					new InvocationHandler() {
						
						@Override
						public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
							Log.i("11webview", "invoke " + method.getName());
							try {
								return method.invoke(mProvider, args);
							} catch (Exception e) {
								throw e.getCause();
							}
						}
					});
			ReflectHelper.setF(webView, "mProvider", proxy);
			
//			final WebViewProvider.ViewDelegate viewDelegate = mProvider.getViewDelegate();
//			Object proxy2 = Proxy.newProxyInstance(WebViewProvider.class.getClassLoader(), 
//					new Class[]{WebViewProvider.ViewDelegate.class}, 
//					new InvocationHandler() {
//						
//						@Override
//						public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//							Log.i("11webview", "invoke " + method.getName());
//							try {
//								return method.invoke(viewDelegate, args);
//							} catch (Exception e) {
//								throw e.getCause();
//							}
//						}
//					});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		web = new WebView(getActivity());
		proxy(web);
		
		
		web.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		
		web.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				System.out.println("onLongClick " + v);
				return true;
			}
		});
		
//		web.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
//
//			@Override
//			public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
//				HitTestResult ret = web.getHitTestResult();
//				System.out.println(ret.getType() + ", " + ret.getExtra());
//				web.getHitRect(r);
//				System.out.println(r);
//			}
//			
//		});
		
		web.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
//				mLoadingView.setVisibility(View.VISIBLE);
//				view.loadUrl(url);
				return false;
			}
		});
		
		web.loadUrl("http://m.baidu.com/");
		
		
		
		return web;
	}

}
